package com.cx.mall.demo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cx.mall.common.model.goods.Product;
import com.cx.mall.common.model.goods.SkuInfo;
import com.cx.mall.common.model.order.OrderInfo;
import com.cx.mall.common.model.user.UserReceiveAddressDTO;
import com.cx.mall.common.utils.ResponseVO;
import com.cx.mall.demo.model.UserReceiveAddress;
import com.cx.mall.demo.service.IDemoOrderInfoImpl;
import com.cx.mall.demo.service.IProductService;
import com.cx.mall.demo.service.IUserReceiveAddressFeignService;
import com.cx.mall.user.service.IUserReceiveAddressService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test/feign")
public class FeignController {
    @Autowired
    private IUserReceiveAddressFeignService userReceiveAddressFeignService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IDemoOrderInfoImpl FeignService;

    @GetMapping("listById/{userId}")
    public ResponseVO listAddress(@PathVariable("userId") Long id, HttpServletRequest request) {
        String auth = request.getHeader("Authorization");
        List<UserReceiveAddress> list = userReceiveAddressFeignService.listById(id, auth);
        return ResponseVO.success(list);
    }

    @GetMapping("/goods/list")
    @SentinelResource(value = "goodsList",fallback = "goodsListFallback")
    public ResponseVO list(Product product) {
        Page page = productService.page(product);
        return ResponseVO.success(page);
    }

    @GetMapping("/goods/skillList")
    public ResponseVO skillList(Product product) {
        Page page = productService.page(product);
        return ResponseVO.success(page);
    }

    @PostMapping("/address/save")
    public ResponseVO save(@RequestBody com.cx.mall.common.model.user.UserReceiveAddress userReceiveAddress) {
        userReceiveAddressFeignService.save(userReceiveAddress);
        return ResponseVO.success();
    }

    @PostMapping("/orderInfo/create")
    public ResponseVO createOrderInfo(@RequestBody OrderInfo orderInfo) {
        FeignService.saveOrderInfo(orderInfo);
        return ResponseVO.success();
    }

    @PostMapping("/orderInfo/seckill")
    public ResponseVO seckillOrderInfo(@RequestBody OrderInfo orderInfo) {
        FeignService.saveOrderInfo(orderInfo);
        return ResponseVO.success();
    }

    @GetMapping("/seckill")
    @SentinelResource(value = "scekillProduct",fallback = "seckillFallback")
    public ResponseVO seckill(@RequestParam("userId") String userId,
                              @RequestParam("skuId") String skuId){
        String id = productService.queryByUserIdAndSkuId(userId,skuId);
        return ResponseVO.success(id);
    }
    // 兜底返回
    ResponseVO seckillFallback( Long userId, Long skuId,Throwable ex){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(0L);
        skuInfo.setSkuName(ex.getMessage());
        skuInfo.setNum(0);
        return ResponseVO.success(skuInfo);
    }

    ResponseVO goodsListFallback(Product product){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(0L);
        skuInfo.setSkuName("未知商品");
        skuInfo.setNum(0);
        Page page = new Page();
        page.setRecords(List.of(skuInfo));
        return ResponseVO.success(skuInfo);
    }

}
